<?php
echo $this->Html->script('nav/switchNavigation');

?>


<h2> Edition des Types de figures </h2>


<div class="switchNavigation">
    <div class="navSwitch">
        
    </div>
    
    <div class="item" id="general">
        <h3 class="title">Général</h3>
        <div class="col5 left">
            <h4 class="">Consulter</h4>

            <table class="ajaxEditing" name="table1">
                <thead>
                    <tr>
                        <td>Multiple</td>
                        <td>Type</td>
                        <td>Spin possibles</td>
                        <td colspan="2">Options</td>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($tricksTypes as $trickType){
                        $id = $trickType['TricksType']['id'];
                        $spins = $trickType['TricksTypesSpin'];
                    ?>
                    <tr>
                        <input class="ajaxEditableValue" type="hidden" name="TricksType.id" value="<?php echo $trickType['TricksType']['id'];?>"/>
                        <td class="ajaxEditableValue" name="TricksType.multiple"> <?php echo $trickType['TricksType']['multiple'];?> </td>  
                        <td class="ajaxEditableValue" name="TricksType.name"> <?php echo $trickType['TricksType']['name'];?> </td>  
                        <td class="ajaxEditableValue" name="TricksTypesSpin.spin">    
                        <?php 
                        $str = '';
                        foreach ($spins as $spin){
                            $str .= $spin['spin'].', ';
                        }
                        echo substr($str,0,-2);
                        ?>
                        </td>
                        <td> <?php echo $this->Html->link('Edit', array('action' => 'admin_edit', $id), array('class' => 'trigger', 'name' => 'edit'));?> </td>  
                        <td> <?php echo $this->Html->link('Delete', array('action' => 'admin_delete', $id), array('class' => 'trigger', 'name' => 'delete'));?> </td> 
                    </tr>   
                    <?php
                    }
                    ?>
                </tbody>
            </table>

        </div>
        <div class="col5 right">
            

            <div class="switchNavigation">
                <div class="navSwitch">

                </div>
                
                <div class="item" id="editTricksType">
                    <h4 class="title"> Edition </h4>
                    <?php

                        echo $this->Form->create('TricksType', array('action' => 'admin_add', 'name' => 'form_table1'));
                        echo $this->Form->input('TricksType.id');
                        echo $this->Form->input("TricksType.name", array(
                            'options' => array(
                                'spin' => 'Spin',
                                'frontflip' => 'Frontflip',
                                'backflip' => 'Backflip',
                                'cork' => 'Cork',
                                'rodeo' => 'Rodéo',
                                'biospin' => 'Biospin',
                                'flatspin' => 'Flatspin',
                            )
                        ));
                        echo $this->Form->input("TricksType.multiple", array(
                            'options' => array(
                                'simple' => 'simple',
                                'double' => 'double',
                                'triple' => 'triple'
                            )
                        ));
                        echo $this->Form->input("TricksTypesSpin.spin",array(
                                'label' => 'Entré les spin possible pour ce trick (séparé par des vigules',
                                'type' => 'text'
                        ));
                        echo $this->Form->end("Add");
                    ?>
                </div>
                <div class="item" id="addTricksType">
                    <h4 class="title"> Ajout </h4>
                     <?php
                        echo $this->Form->create('TricksType', array('action' => 'admin_add'));
                        echo $this->Form->input("TricksType.name", array(
                            'options' => array(
                                'spin' => 'Spin',
                                'frontflip' => 'Frontflip',
                                'backflip' => 'Backflip',
                                'cork' => 'Cork',
                                'rodeo' => 'Rodéo',
                                'biospin' => 'Biospin',
                                'flatspin' => 'Flatspin',
                            )
                        ));
                        echo $this->Form->input("TricksType.multiple", array(
                            'options' => array(
                                'simple' => 'simple',
                                'double' => 'double',
                                'triple' => 'triple'
                            )
                        ));
                        echo $this->Form->input("TricksTypesSpin.spin",array(
                                'label' => 'Entré les spin possible pour ce trick (séparé par des vigules',
                                'type' => 'text'
                        ));
                        echo $this->Form->end("Add");
                    ?>
                </div>
            </div>
        </div>
    </div>
    
    
    <?php
    foreach ($sports as $sport){ 
        $name = $sport['Sport']['name'];
        $sportId = $sport['Sport']['id'];
        
        $tricksTypesDescriptions = $sport['TricksTypesDescription'];
       
    ?>
    <div class="item" id="<?php echo $name;?>">
        <h3 class="title"> <?php echo $name;?> </h3>
        <div class="col5 left">

            <table>
                <thead>
                    <tr>
                        <td></td>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($tricksTypesDescriptions as $tricksTypesDescription){
                        $id = $tricksTypesDescription['id'];
                    ?>
                    <tr>
                        <td> <?php echo $tricksTypesDescription['name'];?> </td>  
                        <td> <?php echo $tricksTypesList[$tricksTypesDescription['tricks_types_id']];?> </td>  
                        
                        <td> <?php echo $this->Html->link('Edit', array('action' => '', $id));?> </td>  
                        <td> <?php echo $this->Html->link('Delete', array('action' => 'admin_delete', $id));?> </td> 
                    </tr>   
                    <?php
                    }
                    ?>
                </tbody>
            </table>
        </div>
        <div class="col5 right">

            <div >
                <?php
                echo $this->Form->create('TricksTypesDescription', array('controller' => 'TricksTypesDescriptions', 'name' => 'form_table1'));
                echo $this->Form->input("TricksTypesDescription.sports_id", array(
                    'value' => $sportId,
                    'type' => 'hidden'
                ));
                echo $this->Form->input("TricksTypesDescription.name");   
                echo $this->Form->input("TricksTypesDescription.description", array(
                    'type' => 'textarea'
                ));        
                echo $this->Form->input("TricksTypesDescription.tricks_types_id", array(
                    'options' => $tricksTypesList
                ));
                echo $this->Form->end("Add");
                ?>
            </div>
        </div>
    </div>
    
    <?php
    }
    ?>
    
</div>


<script type="text/javascript">
    settings = {
        baseUrl : '<?php echo $this->webroot;?>'
    };

    selectors = {
        table : '.ajaxEditing',
        trigger : '.trigger'
    };


    $(document).ready(function(){
        
        $(selectors.table).each(function(){
            var thisTable = $(this);
            var tableName = thisTable.attr('name');
            $(selectors.trigger, thisTable).click(function(){
                
                var thisLink = $(this);
                var href = thisLink.attr('href');
                
                
                var actionType = thisLink.attr('name');
                
                switch (actionType){
                    case 'delete':
                        ajaxDel(href);
                        delRow(thisLink);
                    break;
                    case 'edit':
                        var form = $('form[name="form_'+tableName+'"]');
                        editRow(thisLink, form);
                        
                    break;
                }
                
                
                return false;
        
            });
        });
    });
    
    function ajaxDel(url){
        $.ajax({
            url : url,
            type: 'GET',
            success: function(){
                return true;
            },
            error : function(){
                alert('Sa merde');
                return false;
            }
        });
    }
    
    function editRow(link, form){
    
         var item = getParentRow($(link));
         //var table = getParentTable(item); 
         item.siblings().removeClass('selected');
         
        $('.ajaxEditableValue', item).each(function(){
            var name = $(this).attr('name');
            var info = name.split('.');
            var model = info[0];
            var attr = info[1];
            
            var fieldName = 'data[' + model + '][' + attr + ']';
            
            // Insertion des valeur dans le formulaire : 
            var field = $('[name="'+fieldName+'"]', form);
            
            if (field.is('input[type="text"]')){
                var val = trim($(this).text());
                field.val(val);
            }
            else if (field.is('select')){
                var val = trim($(this).text());
                $('option[value="'+val+'"]', field).attr("selected", "selected");
            }
            else if (field.is('input[type="hidden"]')){
                var val = $(this).val();
                field.val(val);
            }
            else{
                alert('A finir de coder les types de form');
            }
        });
        
        // Changement de stlyle
        item.addClass('selected');
        
    }
    
    function delRow(link){
        var item = getParentRow($(link));        
        item.remove();
    }
    
    function trim (myString)
    {
        return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
    } 
    
    function getParentRow(item){        
        while (!item.is('tr')){
            item = item.parent();
        }
        return item;
    }
    
    function getParentTable(item){   
        while (!item.is('table')){
            item = item.parent();
        }
        return item;
        
    }
</script>